<template>
  <div class="course-created-edit">
    <el-card>
      <div slot="header">
        <el-steps :active="activeStep" finish-status="success">
          <el-step
            :key="item.id"
            v-for="(item, i) in steps"
            :title="item.title"
            @click.native="activeStep = i"
          ></el-step>
        </el-steps>
      </div>
      <el-form label-width="80px">
        <div v-show="activeStep === 0">
          <el-form-item label="课程名称">
            <el-input v-model="course.courseName"></el-input>
          </el-form-item>
          <el-form-item label="课程简介">
            <el-input v-model="course.brief"></el-input>
          </el-form-item>
          <el-form-item label="课程概述">
            <el-input style="width:48%;min-width:300px;margin-right:15px" placeholder="概述1" v-model="course.previewFirstField">
                <template slot="append">{{ course.previewFirstField.length }}/20</template>
            </el-input>
            <el-input style="width:48%;min-width:300px;" placeholder="概述2" v-model="course.previewSecondField"></el-input>
          </el-form-item>
          <el-form-item label="讲师姓名">
            <el-input v-model="course.teacherDTO.teacherName"></el-input>
          </el-form-item>
          <el-form-item label="讲师简介">
            <el-input v-model="course.teacherDTO.description"></el-input>
          </el-form-item>
          <el-form-item label="课程排序">
            <el-input-number
              v-model="course.sortNum"
              controls-position="right"
              :min="1"
              :max="10"
            ></el-input-number>
          </el-form-item>
        </div>
        <div v-show="activeStep === 1">
          <course-image label="课程封面" v-model="course.courseListImg"></course-image>
          <course-image label="解锁封面" v-model="course.courseImgUrl"></course-image>
        </div>
        <div v-show="activeStep === 2">
          <el-form-item label="售卖价格">
            <el-input :min="0" type="number" v-model="course.discounts">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="商品原价">
            <el-input :min="0" type="number" v-model="course.price">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="销量">
            <el-input type="number" :min="0" v-model="course.sales">
              <template slot="append">单</template>
            </el-input>
          </el-form-item>
          <el-form-item label="活动标签">
            <el-input v-model="course.discountsTag">
            </el-input>
          </el-form-item>
        </div>
        <div v-show="activeStep === 3">
            <el-form-item label="限时秒杀开关" label-width="120px">
                <el-switch
                v-model="course.activityCourse" active-color="#13ce66" inactive-color="#fe4949"></el-switch>
            </el-form-item>
            <template v-if="course.activityCourse">
                <el-form-item label="开始时间">
                    <el-date-picker v-model="course.activityCourseDTO.beginTime" type="datetime" placeholder="请选择时间"></el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间">
                    <el-date-picker v-model="course.activityCourseDTO.endTime" type="datetime" placeholder="请选择时间"></el-date-picker>
                </el-form-item>
                <el-form-item label="秒杀价格">
                    <el-input v-model="course.activityCourseDTO.amount">
                        <template slot="append">元</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="库存">
                    <el-input type="number" :min="0" v-model="course.activityCourseDTO.stock">
                        <template slot="append">个</template>
                    </el-input>
                </el-form-item>
            </template>
        </div>
        <div v-show="activeStep === 4">
          <el-form-item label="课程详情">
              <text-editor v-model="course.courseDescriptionMarkDown"></text-editor>
          </el-form-item>
            <el-form-item  label-width="80px" label="上架按钮">
              <el-switch :active-value="1" :inactive-value="0" active-color="#13ce66" inactive-color="#ff4949" v-model="course.status"></el-switch>
            </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSave">保存</el-button>
          </el-form-item>
        </div>
        <el-form-item>
          <el-button v-show="activeStep !== steps.length - 1" @click="activeStep++"
            >下一步</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { saveOrupdate, getCourseById } from '@/services/course'
import courseImage from './courseImage'
import textEditor from '@/components/editor'

export default {
  name: 'courseCreatedEdit',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    courseId: {
      type: [String, Number]
    }
  },
  data () {
    return {
      activeStep: 0,
      steps: [
        { id: 1, title: '基本信息' },
        { id: 2, title: '课程封面' },
        { id: 3, title: '销售信息' },
        { id: 4, title: '秒杀信息' },
        { id: 5, title: '课程详情' }
      ],
      num: 0,
      imageUrl: '',
      course: {
        courseName: '',
        brief: '',
        teacherDTO: {
          teacherName: '',
          teacherHeadPicUrl: '',
          position: '',
          description: ''
        },
        courseDescriptionMarkDown: '',
        price: 0,
        discounts: 0,
        priceTag: '',
        discountsTag: '',
        isNew: true,
        isNewDes: '',
        courseListImg: '',
        courseImgUrl: '',
        sortNum: 0,
        previewFirstField: '',
        previewSecondField: '',
        status: 0,
        sales: 0,
        activityCourse: false,
        activityCourseDTO: {
          beginTime: '',
          endTime: '',
          amount: 0,
          stock: 0
        },
        autoOnlineTime: ''
      }
    }
  },
  components: {
    courseImage,
    textEditor
  },
  created () {
    if (this.isEdit) {
      this.loadCourse()
    }
  },
  methods: {
    async loadCourse () {
      const { data } = await getCourseById(this.courseId)
      if (data.code === '000000') {
        if (!data.data.activityCourse) {
          data.data.activityCourseDTO = {
            beginTime: '',
            endTime: '',
            amount: 0,
            stock: 0
          }
        }
        this.course = data.data
      }
    },
    async handleSave () {
      if (this.course.courseName && this.course.discounts) {
        const { data } = await saveOrupdate(this.course)
        if (data.code === '000000') {
          this.$message.success(`${this.isEdit ? '编辑' : '添加'}课程成功`)
        } else {
          this.$message.error('传递失败')
        }
      } else {
        return this.$message.error('课程名称和销售价格必须填写')
      }
      this.$router.push({ name: 'course' })
    }
  }
}
</script>

<style lang='scss' scoped>
.el-step {
  cursor: pointer;
}
</style>
